//
//  UIViewController+BGTransition.m
//  Nav
//
//  Created by Lam BG on 2018/8/8.
//  Copyright © 2018年 YZ. All rights reserved.
//

#import "UIViewController+BGTransition.h"
#import "UINavigationController+BGTransition.h"

#include <objc/runtime.h>

@implementation UIViewController (BGTransition)

+ (void)load{
    
    SEL originalAppearSEL = @selector(viewWillAppear:);
    SEL swizzledAppearSEL = @selector(bg_viewWillAppear:);
    Method originalAppearMethod = class_getInstanceMethod(self, originalAppearSEL);
    Method swizzledAppearMethod = class_getInstanceMethod(self, swizzledAppearSEL);
    
    method_exchangeImplementations(originalAppearMethod, swizzledAppearMethod);
}

- (void)resetTransitionStyle {
    
    self.bg_navAlpha = self.bg_navAlpha;
    self.bg_navColor = self.bg_navColor;
    self.bg_navTitleColor = self.bg_navTitleColor;
}

- (void)bg_viewWillAppear:(BOOL)animated {
    
    [self bg_viewWillAppear:animated];
    
    if ([self.parentViewController isKindOfClass:[UINavigationController class]] &&
      (!self.navigationController.bg_isEdgePan)) {
        
        [self resetTransitionStyle];
    }
}

#pragma mark - Getter / Setter

// 导航栏背景色
- (UIColor *)bg_navColor {
    
    UIColor *bgColor = objc_getAssociatedObject(self, _cmd);
    bgColor = bgColor == nil ? [UIColor whiteColor] : bgColor;
    
    return bgColor;
}

- (void)setBg_navColor:(UIColor *)bg_navColor {
    
    objc_setAssociatedObject(self, @selector(bg_navColor), bg_navColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    // 设置导航栏背景色
    if (self.navigationController) {
        
        self.navigationController.navigationBar.barTintColor = bg_navColor;
    }
}

#pragma mark -

// 导航栏标题颜色
- (UIColor *)bg_navTitleColor {
    
    UIColor *bgColor = objc_getAssociatedObject(self, _cmd);
    bgColor = bgColor == nil ? [UIColor whiteColor] : bgColor;
    
    return bgColor;
}

- (void)setBg_navTitleColor:(UIColor *)bg_navTitleColor {
    
    objc_setAssociatedObject(self, @selector(bg_navTitleColor), bg_navTitleColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    // 设置导航栏标题颜色
    if (self.navigationController) {
        
        [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : bg_navTitleColor}];
    }
}

#pragma mark -

// 导航栏透明度
- (CGFloat)bg_navAlpha {
    
    // 默认透明度为 1
    CGFloat bgAlpha = 1;
    NSString *alphaStr = objc_getAssociatedObject(self, _cmd);
    if (alphaStr != nil) {
        
        // 获取设置后的透明度值
        bgAlpha = [alphaStr floatValue];
        bgAlpha = MAX(0, MIN(1, bgAlpha));
    }
    
    return bgAlpha;
}

- (void)setBg_navAlpha:(CGFloat)bg_navAlpha {
    
    // 设置时 控制范围
    CGFloat bgAlpha = MAX(0, MIN(1, bg_navAlpha));
    objc_setAssociatedObject(self, @selector(bg_navAlpha), @(bgAlpha), OBJC_ASSOCIATION_COPY_NONATOMIC);
    // 设置导航栏透明度
    if (self.navigationController) {
        
        [self.navigationController bg_updateNavAlpha:bg_navAlpha];
    }
}

#pragma mark -

@end























